#include<iostream>
using namespace std;
int n = 0;
int k;
const int N = 5e4 + 10;
int fa[N*3];//注意别开小了
int ret;
int find(int x)
{
	if (x == fa[x]) return x;
	else return fa[x] = find(fa[x]);
}
void un(int x, int y)
{
	int fx = find(x);
	int fy = find(y);
	if(fx!=fy)
	fa[fx] = fy;
}
int main()
{
	cin >> n >> k;
	for (int i = 1;i <= n;i++)
	{
		//+n捕食域
		//+2n是天敌域
		fa[i] = i;
		fa[i + n] = i + n;
		fa[i + n + n] = i + n + n;
	}
	while (k--)
	{
		int e = 0, x = 0, y = 0;
		cin >> e >> x >> y;
		if (x > n || y > n)
		{
			ret++;continue;
		}
		if (e == 1)
		{
			//同类也得看是不是假话
			if ((find(x) == find(y + n)) || find(x) == find(y + n + n)) 
			{
				ret++;
			}
			//同类
			else
			{
				un(x, y);
				un(x + n, y + n);
				un(x + n + n, y + n + n);
			}
		}
		else
		{
			//看看是不是假话
			//虽然说是x吃x，但是如果xy同类的话也不能吃，这样包括那种情况，更严谨
			if ((find(x)==find(y))||(find(x)==find(y+n))) ret++;
			else
			{
				//注意得把三个对应的线都连上
				un(x, y + n + n);
				un(y, x + n);
				un(x + n + n, y + n);
			}
		}
	}
	cout << ret << endl;
	return 0;
}